﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Objects;
using Microsoft.Practices.Unity;
using VLUSM_BusinessLogic.Helpers;
using VLUSM_BusinessLogic.DataTransferObjects;
using VLUSM_DataAccess;
using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Collections.ObjectModel;

namespace VLUSM_BusinessLogic
{
    public class CatalogueManager
    {
        IUnityContainer container;

        EntityConnection mainDataConnection;
        EntityConnection DVHCConnection;
        EntityConnection diemConnection;

        public CatalogueManager(IUnityContainer container)
        {
            this.container = container;
            this.Initialize();
        }

        #region CauLacBo Methods
        public ObservableCollection<CauLacBoDTO> GetCauLacBoList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.CauLacBo;
                var oc = new ObservableCollection<CauLacBo>(result);
                return Converter.ConvertToCauLacBoDTOList(oc);
            }
        }

        public CauLacBoDTO GetCauLacBoByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.CauLacBo.Single(c => c.ID == IDFilter);
                return Converter.ConvertToCauLacBoDTO(result);
            }
        }

        public CauLacBoDTO GetCauLacBoByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.CauLacBo.Single(c => c.TenGoi == TenFilter);
                return Converter.ConvertToCauLacBoDTO(result);
            }
        }

        public void SaveCauLacBo(CauLacBoDTO objdto)
        {
            var obj = Converter.ConvertToCauLacBo(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.CauLacBo.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveCauLacBoList(ObservableCollection<CauLacBoDTO> dtolist)
        {
            var list = Converter.ConvertToCauLacBoList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.CauLacBo.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveCauLacBo(CauLacBoDTO objdto)
        {
            var obj = Converter.ConvertToCauLacBo(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.CauLacBo.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveCauLacBoByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.CauLacBo.Single(c => c.ID == IDFilter);
                context.CauLacBo.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region ChinhSach Methods
        public ObservableCollection<ChinhSachDTO> GetChinhSachList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.ChinhSach;
                var oc = new ObservableCollection<ChinhSach>(result);
                return Converter.ConvertToChinhSachDTOList(oc);
            }
        }

        public ChinhSachDTO GetChinhSachByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.ChinhSach.Single(c => c.ID == IDFilter);
                return Converter.ConvertToChinhSachDTO(result);
            }
        }

        public ChinhSachDTO GetChinhSachByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.ChinhSach.Single(c => c.TenChinhSach == TenFilter);
                return Converter.ConvertToChinhSachDTO(result);
            }
        }

        public void SaveChinhSach(ChinhSachDTO objdto)
        {
            var obj = Converter.ConvertToChinhSach(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.ChinhSach.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveChinhSachList(ObservableCollection<ChinhSachDTO> dtolist)
        {
            var list = Converter.ConvertToChinhSachList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.ChinhSach.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveChinhSach(ChinhSachDTO objdto)
        {
            var obj = Converter.ConvertToChinhSach(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.ChinhSach.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveChinhSachByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.ChinhSach.Single(c => c.ID == IDFilter);
                context.ChinhSach.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region DanToc Methods
        public ObservableCollection<DanTocDTO> GetDanTocList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DanToc;
                var oc = new ObservableCollection<DanToc>(result);
                return Converter.ConvertToDanTocDTOList(oc);
            }
        }

        public DanTocDTO GetDanTocByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DanToc.Single(d => d.ID == IDFilter);
                return Converter.ConvertToDanTocDTO(result);
            }
        }

        public DanTocDTO GetDanTocByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DanToc.Single(d => d.TenGoi == TenFilter);
                return Converter.ConvertToDanTocDTO(result);
            }
        }

        public void SaveDanToc(DanTocDTO objdto)
        {
            var obj = Converter.ConvertToDanToc(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.DanToc.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveDanTocList(ObservableCollection<DanTocDTO> dtolist)
        {
            var list = Converter.ConvertToDanTocList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.DanToc.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveDanToc(DanTocDTO objdto)
        {
            var obj = Converter.ConvertToDanToc(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.DanToc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveDanTocByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.DanToc.Single(h => h.ID == IDFilter);
                context.DanToc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region DonVi Methods
        public ObservableCollection<DonViDTO> GetDonViList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DonVi;
                var oc = new ObservableCollection<DonVi>(result);
                return Converter.ConvertToDonViDTOList(oc);
            }
        }

        public ObservableCollection<DonViDTO> GetDonViListByCapTruong()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DonVi.Where(d => d.IsCapTruong);
                var oc = new ObservableCollection<DonVi>(result);
                return Converter.ConvertToDonViDTOList(oc);
            }
        }

        public ObservableCollection<DonViDTO> GetDonViListByCapDonVi()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DonVi.Where(d => !d.IsCapTruong);
                var oc = new ObservableCollection<DonVi>(result);
                return Converter.ConvertToDonViDTOList(oc);
            }
        }

        public DonViDTO GetDonViByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DonVi.Single(d => d.ID == IDFilter);
                return Converter.ConvertToDonViDTO(result);
            }
        }

        public DonViDTO GetDonViByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.DonVi.Single(d => d.TenGoi == TenFilter);
                return Converter.ConvertToDonViDTO(result);
            }
        }

        public void SaveDonVi(DonViDTO objdto)
        {
            var obj = Converter.ConvertToDonVi(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.DonVi.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveDonViList(ObservableCollection<DonViDTO> dtolist)
        {
            var list = Converter.ConvertToDonViList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.DonVi.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveDonVi(DonViDTO objdto)
        {
            var obj = Converter.ConvertToDonVi(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.DonVi.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveDonViByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.DonVi.Single(h => h.ID == IDFilter);
                foreach (var quyen in obj.Quyens.ToList())
                {
                    obj.Quyens.Remove(quyen);
                }
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region DVHCCap1 Methods
        public ObservableCollection<DVHCCap1DTO> GetDVHCCap1List()
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap1;
                var oc = new ObservableCollection<DVHCCap1>(result);
                return Converter.ConvertToDVHCCap1DTOList(oc);
            }
        }

        public DVHCCap1DTO GetDVHCCap1ByID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap1.Single(d => d.ID == IDFilter);
                return Converter.ConvertToDVHCCap1DTO(result);
            }
        }

        public DVHCCap1DTO GetDVHCCap1ByTen(string TenFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap1.Single(d => d.Ten == TenFilter);
                return Converter.ConvertToDVHCCap1DTO(result);
            }
        }

        public DVHCCap1DetailDTO GetDVHCCap1DetailByID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var query = context.DVHCCap1.Single(d => d.ID == IDFilter);
                var result = Converter.ConvertToDVHCCap1DetailDTO(query);
                var query2 = new ObservableCollection<DVHCCap2>(context.DVHCCap2.Where(d => d.Cap1ID == result.ID));
                var result2 = Converter.ConvertToDVHCCap2DTOList(query2);
                result.DVHCCap2 = result2;
                return result;
            }
        }

        public DVHCCap1DTO GetDVHCCap1ByMaDonVi(string MaDonViFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap1.Single(d => d.MaDonVi == MaDonViFilter);
                return Converter.ConvertToDVHCCap1DTO(result);
            }
        }

        public void SaveDVHCCap1(DVHCCap1DTO objdto)
        {
            var obj = Converter.ConvertToDVHCCap1(objdto);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                context.DVHCCap1.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveDVHCCap1List(ObservableCollection<DVHCCap1DTO> dtolist)
        {
            var list = Converter.ConvertToDVHCCap1List(dtolist);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                foreach (var obj in list)
                {
                    context.DVHCCap1.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveDVHCCap1(DVHCCap1DTO objdto)
        {
            var obj = Converter.ConvertToDVHCCap1(objdto);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                context.DVHCCap1.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveDVHCCap1ByID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var obj = context.DVHCCap1.Single(h => h.ID == IDFilter);
                context.DVHCCap1.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region DVHCCap2 Methods
        public ObservableCollection<DVHCCap2DTO> GetDVHCCap2List()
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2;
                var oc = new ObservableCollection<DVHCCap2>(result);
                return Converter.ConvertToDVHCCap2DTOList(oc);
            }
        }

        public ObservableCollection<DVHCCap2DTO> GetDVHCCap2ListByDVHCCap1ID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Where(d => d.Cap1ID == IDFilter);
                var oc = new ObservableCollection<DVHCCap2>(result);
                return Converter.ConvertToDVHCCap2DTOList(oc);
            }
        }

        public ObservableCollection<DVHCCap2DTO> GetDVHCCap2ListByDVHCCap1MaDonVi(string MaDonViFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Where(d => d.Cap1.MaDonVi == MaDonViFilter);
                var oc = new ObservableCollection<DVHCCap2>(result);
                return Converter.ConvertToDVHCCap2DTOList(oc);
            }
        }

        public ObservableCollection<DVHCCap2DTO> GetDVHCCap2ListByDVHCCap1Ten(string TenFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Where(d => d.Cap1.Ten == TenFilter);
                var oc = new ObservableCollection<DVHCCap2>(result);
                return Converter.ConvertToDVHCCap2DTOList(oc);
            }
        }

        public DVHCCap2DTO GetDVHCCap2ByID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Single(d => d.ID == IDFilter);
                return Converter.ConvertToDVHCCap2DTO(result);
            }
        }

        public DVHCCap2DTO GetDVHCCap2ByTen(string TenFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Single(d => d.Ten == TenFilter);
                return Converter.ConvertToDVHCCap2DTO(result);
            }
        }

        public DVHCCap2DTO GetDVHCCap2ByMaDonVi(string MaDonViFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var result = context.DVHCCap2.Single(d => d.MaDonVi == MaDonViFilter);
                return Converter.ConvertToDVHCCap2DTO(result);
            }
        }

        public void SaveDVHCCap2(DVHCCap2DTO objdto)
        {
            var obj = Converter.ConvertToDVHCCap2(objdto);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                context.DVHCCap2.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveDVHCCap2List(ObservableCollection<DVHCCap2DTO> dtolist)
        {
            var list = Converter.ConvertToDVHCCap1List(dtolist);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                foreach (var obj in list)
                {
                    context.DVHCCap2.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveDVHCCap2(DVHCCap2DTO objdto)
        {
            var obj = Converter.ConvertToDVHCCap2(objdto);
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                context.DVHCCap2.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveDVHCCap2ByID(int IDFilter)
        {
            using (var context = container.Resolve<DonViHanhChinhContainer>())
            {
                var obj = context.DVHCCap2.Single(h => h.ID == IDFilter);
                context.DVHCCap2.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region HoatDongDoanThe Methods
        public ObservableCollection<HoatDongDoanTheDTO> GetHoatDongDoanTheList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.HoatDongDoanThe;
                var oc = new ObservableCollection<HoatDongDoanThe>(result);
                return Converter.ConvertToHoatDongDoanTheDTOList(oc);
            }
        }

        public HoatDongDoanTheDTO GetHoatDongDoanTheByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.HoatDongDoanThe.Single(h => h.ID == IDFilter);
                return Converter.ConvertToHoatDongDoanTheDTO(result);
            }
        }

        public HoatDongDoanTheDTO GetHoatDongDoanTheByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.HoatDongDoanThe.Single(h => h.TenGoi == TenFilter);
                return Converter.ConvertToHoatDongDoanTheDTO(result);
            }
        }

        public void SaveHoatDongDoanThe(HoatDongDoanTheDTO objdto)
        {
            var obj = Converter.ConvertToHoatDongDoanThe(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.HoatDongDoanThe.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveHoatDongDoanTheList(ObservableCollection<HoatDongDoanTheDTO> dtolist)
        {
            var list = Converter.ConvertToHoatDongDoanTheList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.HoatDongDoanThe.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveHoatDongDoanThe(HoatDongDoanTheDTO objdto)
        {
            var obj = Converter.ConvertToHoatDongDoanThe(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.HoatDongDoanThe.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveHoatDongDoanTheByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.HoatDongDoanThe.Single(h => h.ID == IDFilter);
                context.HoatDongDoanThe.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region KhoaHoc Methods
        public ObservableCollection<KhoaHocDTO> GetKhoaHocList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.KhoaHoc;
                var oc = new ObservableCollection<KhoaHoc>(result);
                return Converter.ConvertToKhoaHocDTOList(oc);
            }
        }

        public KhoaHocDTO GetKhoaHocByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.KhoaHoc.Single(k => k.ID == IDFilter);
                return Converter.ConvertToKhoaHocDTO(result);
            }
        }

        public KhoaHocDTO GetKhoaHocByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.KhoaHoc.Single(k => k.TenGoi == TenFilter);
                return Converter.ConvertToKhoaHocDTO(result);
            }
        }

        public KhoaHocDTO GetKhoaHocByNamNhapHoc(int NamNhapHocFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.KhoaHoc.Single(k => k.NamNhapHoc == NamNhapHocFilter);
                return Converter.ConvertToKhoaHocDTO(result);
            }
        }

        public void SaveKhoaHoc(KhoaHocDTO objdto)
        {
            var obj = Converter.ConvertToKhoaHoc(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.KhoaHoc.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveKhoaHocList(ObservableCollection<KhoaHocDTO> dtolist)
        {
            var list = Converter.ConvertToKhoaHocList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.KhoaHoc.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveKhoaHoc(KhoaHocDTO objdto)
        {
            var obj = Converter.ConvertToKhoaHoc(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.KhoaHoc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveKhoaHocByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.KhoaHoc.Single(h => h.ID == IDFilter);
                context.KhoaHoc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region LoaiVanBang Methods
        public ObservableCollection<LoaiVanBangDTO> GetLoaiVanBangList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiVanBang;
                var oc = new ObservableCollection<LoaiVanBang>(result);
                return Converter.ConvertToLoaiVanBangDTOList(oc);
            }
        }

        public LoaiVanBangDTO GetLoaiVanBangByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiVanBang.Single(v => v.ID == IDFilter);
                return Converter.ConvertToLoaiVanBangDTO(result);
            }
        }

        public LoaiVanBangDTO GetLoaiVanBangByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiVanBang.Single(v => v.TenGoi == TenFilter);
                return Converter.ConvertToLoaiVanBangDTO(result);
            }
        }

        public void SaveLoaiVanBang(LoaiVanBangDTO objdto)
        {
            objdto.NgayTao = DateTime.Today;
            var obj = Converter.ConvertToLoaiVanBang(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.LoaiVanBang.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveLoaiVanBangList(ObservableCollection<LoaiVanBangDTO> dtolist)
        {
            var list = Converter.ConvertToLoaiVanBangList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    obj.NgayTao = DateTime.Today;
                    context.LoaiVanBang.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveLoaiVanBang(LoaiVanBangDTO objdto)
        {
            var obj = Converter.ConvertToLoaiVanBang(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.LoaiVanBang.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveLoaiVanBangByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.LoaiVanBang.Single(v => v.ID == IDFilter);
                context.LoaiVanBang.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region Lop Methods
        public ObservableCollection<LopDTO> GetLopList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop;
                var oc = new ObservableCollection<Lop>(result);
                return Converter.ConvertToLopDTOList(oc);
            }
        }

        public ObservableCollection<LopDTO> GetLopListByDonViID(int DonViIDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Where(l => l.DonViID == DonViIDFilter);
                var oc = new ObservableCollection<Lop>(result);
                return Converter.ConvertToLopDTOList(oc);
            }
        }

        public ObservableCollection<LopDTO> GetLopListByKhoaHocID(int KhoaHocIDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Where(l => l.KhoaHocID == KhoaHocIDFilter);
                var oc = new ObservableCollection<Lop>(result);
                return Converter.ConvertToLopDTOList(oc);
            }
        }

        public ObservableCollection<LopDTO> GetLopListByDonViandKhoaHocID(int DonViIDFilter, int KhoaHocIDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Where(l => (l.DonViID == DonViIDFilter && l.KhoaHocID == KhoaHocIDFilter));
                var oc = new ObservableCollection<Lop>(result);
                return Converter.ConvertToLopDTOList(oc);
            }
        }

        public LopDTO GetLopByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Single(l => l.ID == IDFilter);
                return Converter.ConvertToLopDTO(result);
            }
        }

        public LopDTO GetLopByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Single(l => l.TenGoi == TenFilter);
                return Converter.ConvertToLopDTO(result);
            }
        }

        public LopDTO GetLopByMaLop(string MaLopFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.Lop.Single(l => l.MaLop == MaLopFilter);
                return Converter.ConvertToLopDTO(result);
            }
        }

        public void SaveLop(LopDTO objdto)
        {
            var obj = Converter.ConvertToLop(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.Lop.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveLopList(ObservableCollection<LopDTO> dtolist)
        {
            var list = Converter.ConvertToLopList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.Lop.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveLop(LopDTO objdto)
        {
            var obj = Converter.ConvertToLop(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.Lop.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveLopByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.Lop.Single(l => l.ID == IDFilter);
                context.Lop.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region MonHoc Methods
        public ObservableCollection<MonHocDTO> GetMonHocList()
        {
            using (var context = container.Resolve<DiemContainer>())
            {
                var result = context.MonHoc;
                var oc = new ObservableCollection<MonHoc>(result);
                return Converter.ConvertToMonHocDTOList(oc);
            }
        }

        //public IEnumerable<MonHocDTO> GetMonHocListByNhomNguoiDung(string NhomNguoiDungFilter)
        //{
        //    using (var repo = container.Resolve<MonHocRepository>())
        //    {
        //        var result = repo.GetAllByNhomNguoiDung(NhomNguoiDungFilter);
        //        return Converter.ConvertToMonHocDTOList(result);
        //    }
        //}

        public MonHocDTO GetMonHocByID(int IDFilter)
        {
            using (var context = container.Resolve<DiemContainer>())
            {
                var result = context.MonHoc.Single(m => m.ID == IDFilter);
                return Converter.ConvertToMonHocDTO(result);
            }
        }

        public MonHocDTO GetMonHocByMaMonHoc(string MaMonHocFilter)
        {
            using (var context = container.Resolve<DiemContainer>())
            {
                var result = context.MonHoc.Single(m => m.MaMonHoc == MaMonHocFilter);
                return Converter.ConvertToMonHocDTO(result);
            }
        }

        public MonHocDTO GetMonHocByTen(string TenFilter)
        {
            using (var context = container.Resolve<DiemContainer>())
            {
                var result = context.MonHoc.Single(m => m.TenGoi == TenFilter);
                return Converter.ConvertToMonHocDTO(result);
            }
        }

        public void SaveMonHoc(MonHocDTO objdto)
        {
            if (objdto.NgayTao == null)
            {
                objdto.NgayTao = DateTime.Today;
            }
            else
            {
                objdto.NgayCapNhat = DateTime.Today;
            }
            var obj = Converter.ConvertToMonHoc(objdto);
            using (var context = container.Resolve<DiemContainer>())
            {
                context.MonHoc.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveMonHocList(ObservableCollection<MonHocDTO> dtolist)
        {
            var list = Converter.ConvertToMonHocList(dtolist);
            using (var context = container.Resolve<DiemContainer>())
            {
                foreach (var obj in list)
                {
                    if (obj.NgayTao == null)
                    {
                        obj.NgayTao = DateTime.Today;
                    }
                    else
                    {
                        obj.NgayCapNhat = DateTime.Today;
                    }
                    context.MonHoc.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveMonHoc(MonHocDTO objdto)
        {
            var obj = Converter.ConvertToMonHoc(objdto);
            using (var context = container.Resolve<DiemContainer>())
            {
                context.MonHoc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveMonHocByID(int IDFilter)
        {
            using (var context = container.Resolve<DiemContainer>())
            {
                var obj = context.MonHoc.Single(h => h.ID == IDFilter);
                context.MonHoc.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region LoaiThayDoi Methods
        public ObservableCollection<LoaiThayDoiDTO> GetLoaiThayDoiList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiThayDoi;
                var oc = new ObservableCollection<LoaiThayDoi>(result);
                return Converter.ConvertToLoaiThayDoiDTOList(oc);
            }
        }

        public LoaiThayDoiDTO GetLoaiThayDoiByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiThayDoi.Single(t => t.ID == IDFilter);
                return Converter.ConvertToLoaiThayDoiDTO(result);
            }
        }

        public LoaiThayDoiDTO GetLoaiThayDoiByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.LoaiThayDoi.Single(t => t.TenGoi == TenFilter);
                return Converter.ConvertToLoaiThayDoiDTO(result);
            }
        }
        #endregion

        #region TonGiao Methods
        public ObservableCollection<TonGiaoDTO> GetTonGiaoList()
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.TonGiao;
                var oc = new ObservableCollection<TonGiao>(result);
                return Converter.ConvertToTonGiaoDTOList(oc);
            }
        }

        public TonGiaoDTO GetTonGiaoByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.TonGiao.Single(t => t.ID == IDFilter);
                return Converter.ConvertToTonGiaoDTO(result);
            }
        }

        public TonGiaoDTO GetTonGiaoByTen(string TenFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.TonGiao.Single(t => t.TenGoi == TenFilter);
                return Converter.ConvertToTonGiaoDTO(result);
            }
        }

        public void SaveTonGiao(TonGiaoDTO objdto)
        {
            var obj = Converter.ConvertToTonGiao(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.TonGiao.Attach(obj);
                if (obj.ID != 0)
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                }
                else
                {
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                }
                context.SaveChanges();
            }
        }

        public void SaveTonGiaoList(ObservableCollection<TonGiaoDTO> dtolist)
        {
            var list = Converter.ConvertToTonGiaoList(dtolist);
            using (var context = container.Resolve<MainDataContainer>())
            {
                foreach (var obj in list)
                {
                    context.TonGiao.Attach(obj);
                    if (obj.ID != 0)
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                    }
                    else
                    {
                        context.ObjectStateManager.ChangeObjectState(obj, EntityState.Added);
                    }
                }
                context.SaveChanges();
            }
        }

        public void RemoveTonGiao(TonGiaoDTO objdto)
        {
            var obj = Converter.ConvertToTonGiao(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.TonGiao.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public void RemoveTonGiaoByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var obj = context.TonGiao.Single(t => t.ID == IDFilter);
                context.TonGiao.Attach(obj);
                context.ObjectStateManager.ChangeObjectState(obj, EntityState.Deleted);
                context.SaveChanges();
            }
        }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            DVHCConnection = EntityConnectionCreator.Instance.DonViHanhChinhConnection;
            mainDataConnection = EntityConnectionCreator.Instance.MainDataConnection;
            diemConnection = EntityConnectionCreator.Instance.DiemConnection;

            this.UnityRegisterType();
            this.InitializeMapper();
        }

        private void UnityRegisterType()
        {
            container.RegisterType<MainDataContainer>(new InjectionConstructor(mainDataConnection));
            container.RegisterType<DonViHanhChinhContainer>(new InjectionConstructor(DVHCConnection));
            container.RegisterType<DiemContainer>(new InjectionConstructor(diemConnection));
        }

        private void InitializeMapper()
        {
            Converter.ConfigureCauLacBoMapping();
            Converter.ConfigureDanTocMapping();
            Converter.ConfigureDonViMapping();
            Converter.ConfigureDVHCCap1Mapping();
            Converter.ConfigureDVHCCap2Mapping();
            Converter.ConfigureHoatDongDoanTheMapping();
            Converter.ConfigureKhoaHocMapping();
            Converter.ConfigureLoaiVanBangMapping();
            Converter.ConfigureLopMapping();
            Converter.ConfigureMonHocMapping();
            Converter.ConfigureTonGiaoMapping();
            Converter.ConfigureLoaiThayDoiMapping();
        }
        #endregion
    }
}
